package jjn.round1;

/**
 * @author Jjn
 * @since 2022-08-06 15:50
 */
public class LeetCode69_SqrtX {
    
    public int mySqrt(int x) {
        int low = 0, high = x / 2;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            long calc = (long) mid * mid;
            long next = (long) (mid + 1) * (mid + 1);
            if (calc <= x && next > x) {
                return mid;
            } else if (calc > x) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return low;
    }
}
